<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of Label
 *
 * @author chaporgin_a
 */
class VA_Form_Decorator_Label extends VA_Form_Decorator {
  const PREPEND = 1;
  const APPEND = 2;
  private $position;
  /**
   *
   * @param int $position position according to content
   */
  public function  __construct($options) {
    $this->position = $this->getOption('position', $options, self::PREPEND);
  }
  public function render($content) {
    $label = $this->getElement()->getLabel();
    if(empty($label)) return $content;
    $for = $this->getElement()->getHTMLTagAttribute('id', TRUE);
    $class = '';
    if(strpos($this->getElement()->getHTMLTagAttribute('class', TRUE), 'required') !== FALSE){
      $class = 'class="required"';
      $label .= ' <span class="required">*</span>';
    }

    $width = $this->getElement()->getHTMLTagAttribute('label_width', TRUE);
    if($width) {
      $width = "width: {$width}px;";
    }
    $height = $this->getElement()->getHTMLTagAttribute('label_height', TRUE);
    if($height) {
      $height = "height: {$height}px;";
    }
    $style = trim(join(' ', array($width, $height)));
    if($style) {
      $style = "style=\"$style\"";
    }

    $label = sprintf('<div class="label" %s><label %s for="%s">%s</label></div>', $style, $class, $for, $label);
    if($this->position === self::APPEND) return $content.$label;
    else return $label.$content;
  }
}